package com.study.testShiro.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.study.testShiro.service.ILoginService;
import com.study.testShiro.utils.ReturnMsg;

@Controller
public class HomeController {

	@Autowired
	private ILoginService loginService;
	
	@RequestMapping({"/","/index"})
    public String index(){
        return"/index";
    }

    @RequestMapping("/403")
    public String unauthorizedRole(){
        System.out.println("------没有权限-------");
        return "/403";
    }

    @RequestMapping(value = "/login")
    public String toLogin()
    {
    	System.out.println("进入登录界面。。。");
        loginService.logout();
        return "/login";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(Map<String, Object> map,HttpServletRequest request) throws Exception{
        System.out.println("login()");
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");

        ReturnMsg rm = loginService.login(userName,password);
        if(rm.isFlag())
        {
            return "/index";
        }
        else {
            map.put("msg",rm.getMsg());
            map.put("userName",userName);
            return "/login";
        }
    }

    @RequestMapping("/logout")
    public String logOut(HttpSession session) {
        loginService.logout();
        return "/login";
    }
}
